package com.yupi.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yupi.mianshiya.constant.CommonConstant;
import com.yupi.mianshiya.model.dto.paper.PaperQueryRequest;
import com.yupi.mianshiya.model.entity.Paper;
import com.yupi.mianshiya.service.PaperService;
import com.yupi.mianshiya.mapper.PaperMapper;
import com.yupi.mianshiya.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper>
    implements PaperService{

    @Resource
    private PaperMapper paperMapper;
    @Override
    public List<Paper> listRandom(int limit, long questionbankid) {
        if (limit <= 0 || limit > 50) {
            limit = 5;
        }
        return baseMapper.listRandom(limit,questionbankid);
    }

    @Override
    public QueryWrapper<Paper> getQueryWrapper(PaperQueryRequest paperQueryRequest) {
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        if (paperQueryRequest == null) {
            return queryWrapper;
        }
        Long id = paperQueryRequest.getId();
        Long questionbankid = paperQueryRequest.getQuestionbankid();
        String content = paperQueryRequest.getContent();
        Long userId = paperQueryRequest.getUserId();
        String sortField = paperQueryRequest.getSortField();
        String sortOrder = paperQueryRequest.getSortOrder();
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(questionbankid), "questionbankid", questionbankid);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);

        //模糊查询
        queryWrapper.like(ObjectUtils.isNotEmpty(content), "content", content);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }

}




